package PenguinsFarm.Penguins.Stage
{
	import PenguinsFarm.Penguins.PenguinsRenderer;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.Timer;

	public class JumpStage implements IPenguinsStage
	{
		private var target:PenguinsRenderer;
		private var timeFactor:Number		=	0;
		private var velocityFactor:Number	=	0;
		
		private var fromVec:Vector3D;
		private var toVec:Vector3D;		
		private var difVec:Vector3D;
		
		private var high:int;
		private var distance:int;
		
		private var savePos:Point;
		private var isComplete:Boolean;
		private var timer:Timer;
		
		public function JumpStage( target:PenguinsRenderer, high:int, distance:int )
		{
			this.savePos	=	new Point( target.x, target.y );
			
			this.target	=	target;
			this.high 	=	high;
			this.distance	=	distance;
			
			this.isComplete = false;
			
			jump();
		}
		
		public function onFrameUpdate(deltaTime:Number):void
		{
			timeFactor	+=	deltaTime * velocityFactor;
			
			var scaleVec:Vector3D	=	difVec.clone();
			scaleVec.scaleBy( timeFactor );
			
			var curVec:Vector3D	=	fromVec.add( scaleVec );
			target.x	=	curVec.x;
			target.y	=	curVec.y;
		}
		
		private function jump():void
		{
			fromVec 	=	new Vector3D( target.x, target.y );
			toVec		=	new Vector3D( fromVec.x + distance/2, fromVec.y - high );
			difVec		=	toVec.subtract( fromVec );
			velocityFactor	=	250.0 / difVec.length;
			
			target.addEventListener( Event.ENTER_FRAME, onMeetHigh );
		}
		
		protected function onMeetHigh(event:Event):void
		{
			// fall down
			if( target.y <= toVec.y )
			{
				target.removeEventListener( Event.ENTER_FRAME, onMeetHigh );
				fall();
				
				// decrease food in lake
				//Global.penguinsCageScene.lake.logicalObj.feed( -20 );
			}
		}
		
		private function fall():void
		{
			fromVec.x 	=	target.x;
			fromVec.y	=	target.y;
			toVec.x		=	toVec.x + distance/2;
			toVec.y		=	toVec.y + high;
			difVec		=	toVec.subtract( fromVec );
			velocityFactor	=	180.0 / difVec.length;
			timeFactor	=	0;
			target.addEventListener( Event.ENTER_FRAME, onMeetGround );
		}
		
		protected function onMeetGround(event:Event):void
		{
			// fall down
			if( target.y >= toVec.y )
			{
				target.removeEventListener( Event.ENTER_FRAME, onMeetGround );
				velocityFactor 	= 	0;
				target.visible 	=	false;
				
				// Make penguins visible for a time like swimming :)
				this.timer		= 	new Timer( 5000 );
				timer.addEventListener( TimerEvent.TIMER, onMeetTimer );
				timer.start();				
			}
		}
		
		protected function onMeetTimer(event:TimerEvent):void
		{			
			timer.stop();
			
			target.x	=	savePos.x;
			target.y	=	savePos.y;
			
			target.visible	= true;
			
			isComplete	=	true;
		}
		
		public function isCompleteStage():Boolean
		{
			return isComplete;
		}
		
		public function dispose():void
		{
			timer 	=	null;
			fromVec	=	null;
			toVec	=	null;
			difVec	=	null;
		}
	}
}